<?php

namespace App\Models;

use Cache;

class Category extends BaseModel
{
    const CACHE_CATEGORY_ALL = 'cache_category_all';
    const CACHE_CATEGORY_TREE = 'cache_category_tree';

    protected $table = 'category';


    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     * 添加数据返回ID
     */
    public function add(array $data)
    {
        $res = $this->insertGetId($data);
        $this->generateCache();
        return $res;
    }


    /**
     * 根据条件更新数据
     */
    public function up(array $where, array $data)
    {
        $res = $this->multiWhere($where)->update($data);
        $this->generateCache();
        return $res;
    }

    /**
     * 重新生成缓存
     */
    public function generateCache()
    {
        Cache::forget(self::CACHE_CATEGORY_ALL);
        $where['show'] = 1;
        $order['sort'] = 'asc';
        $list = $this->getList($where,'*',$order);
        $arr = '';
        if ($list) {
            $list = $list->toArray();
            foreach ($list as $v) {
                $arr[$v['id']] = $v;
            }
        }
        Cache::forever(self::CACHE_CATEGORY_ALL, $arr);
        Cache::forget(self::CACHE_CATEGORY_TREE);
        $tree = $this->getCategoryTree();
        Cache::forever(self::CACHE_CATEGORY_TREE, $tree);
    }

    /**
     * 获取缓存
     */
    public function getCacheCategoryList()
    {
        Cache::forget(self::CACHE_CATEGORY_ALL);
        if (Cache::has(self::CACHE_CATEGORY_ALL)) {
            return Cache::get(self::CACHE_CATEGORY_ALL);
        } else {
            $where['show'] = 1;
            $order['sort'] = 'asc';
            $list = $this->getList($where,'*',$order);
            $arr = '';
            if ($list) {
                $list = $list->toArray();
                foreach ($list as $v) {
                    $arr[$v['id']] = $v;
                }
            }
            Cache::forever(self::CACHE_CATEGORY_ALL, $arr);
            return $arr;
        }
    }

    public function getCategoryLevelTree($pid = 0)
    {
        $categoryList = $this->getCacheCategoryList();
        $categoryTree = levelTree($categoryList, 'id', 'parentid', $pid);
        return $categoryTree;
    }


    public function getCategoryTree()
    {
        if (Cache::has(self::CACHE_CATEGORY_TREE)) {
            return Cache::get(self::CACHE_CATEGORY_TREE);
        } else {
            $list = $this->getCategoryLevelTree();
            if (!$list) {
                return false;
            }
            $list = sortArrayAsc(mult_unique($list), 'sort');
            $tree = $list;
            //对分类数组进行排序
            foreach ($tree as $k => $v) {
                if (isset($v['child']) && is_array($v['child'])) {
                    $tree[$k]['child'] = sortArrayAsc(mult_unique($v['child']));
                    if (isset($tree[$k]['child']) && is_array($tree[$k]['child'])) {
                        foreach ($tree[$k]['child'] as $j => $v1) {
                            if (isset($v1['child']) && is_array($v1['child'])) {
                                $tree[$k]['child'][$j]['child'] = sortArrayAsc(mult_unique($v1['child']));
                                if (isset($tree[$k]['child'][$j]['child']) && is_array($tree[$k]['child'][$j]['child'])) {
                                    foreach ($tree[$k]['child'][$j]['child'] as $m => $n) {
                                        if (isset($n['child']) && is_array($n['child'])) {
                                            $tree[$k]['child'][$j]['child'][$m]['child'] = sortArrayAsc(mult_unique($n['child']));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Cache::forever(self::CACHE_CATEGORY_TREE, $tree);
            return $tree;
        }
    }

    public function getChild($request)
    {
        $pid = $request['pid'];
        if (!$pid) {
            $pid = 0;
        }
        return $this->getCategoryLevelTree($pid);
    }

}
